package com.jspxcms.plug.service.impl;

import com.jspxcms.common.orm.Limitable;
import com.jspxcms.common.orm.RowSide;
import com.jspxcms.common.orm.SearchFilter;
import com.jspxcms.core.domain.Site;
import com.jspxcms.core.listener.SiteDeleteListener;
import com.jspxcms.core.service.SiteService;
import com.jspxcms.plug.domain.PlugDisease;
import com.jspxcms.plug.repository.PlugDiseaseDao;
import com.jspxcms.plug.service.PlugDiseaseService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class PlugDiseaseServiceImpl implements PlugDiseaseService {
	public Page<PlugDisease> findAll(Integer siteId, Map<String, String[]> params,
			Pageable pageable) {
		return dao.findAll(spec( params), pageable);
	}

	public RowSide<PlugDisease> findSide(Integer siteId,
			Map<String, String[]> params, PlugDisease bean, Integer position,
			Sort sort) {
		if (position == null) {
			return new RowSide<PlugDisease>();
		}
		Limitable limit = RowSide.limitable(position, sort);
		List<PlugDisease> list = dao.findAll(spec( params), limit);
		return RowSide.create(list, bean);
	}

	private Specification<PlugDisease> spec(
			Map<String, String[]> params) {
		Collection<SearchFilter> filters = SearchFilter.parse(params).values();
		final Specification<PlugDisease> fsp = SearchFilter.spec(filters,
				PlugDisease.class);
		Specification<PlugDisease> sp = new Specification<PlugDisease>() {
			public Predicate toPredicate(Root<PlugDisease> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate pred = fsp.toPredicate(root, query, cb);

				return pred;
			}
		};
		return sp;
	}

	public List<PlugDisease> findList(Integer[] siteId, Limitable limitable) {
		return dao.getList(siteId, limitable);
	}

	public PlugDisease get(Integer id) {
		return dao.findOne(id);
	}

	@Transactional
	public PlugDisease save(PlugDisease bean, Integer siteId) {
		bean = dao.save(bean);
		return bean;
	}

	@Transactional
	public PlugDisease update(PlugDisease bean) {
		bean = dao.save(bean);
		return bean;
	}

	@Transactional
	public PlugDisease delete(Integer id) {
		PlugDisease entity = dao.findOne(id);
		dao.delete(entity);
		return entity;
	}

	@Transactional
	public PlugDisease[] delete(Integer[] ids) {
		PlugDisease[] beans = new PlugDisease[ids.length];
		for (int i = 0; i < ids.length; i++) {
			beans[i] = delete(ids[i]);
		}
		return beans;
	}


	private PlugDiseaseDao dao;

	@Autowired
	public void setDao(PlugDiseaseDao dao) {
		this.dao = dao;
	}
}
